﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Runtime.InteropServices;

namespace FWMS
{
    public partial class Form_main : Form
    {
        [DllImport("User32.dll")]
        public static extern int GetSystemMenu(int hWnd, int bRevert);
        [DllImport("User32.dll")]
        public static extern int RemoveMenu(int hMenu, int nPosition, int wFlags);
        public Form_main()
        {
            InitializeComponent();
        }

        private void Form_main_Load(object sender, EventArgs e)
        {
            //this.skinEngine1.SkinFile = "OneBlue.ssk";
            //this.ControlBox = false;
            timer1.Start();
           // child[0] = new Form_cailiaoguanli();
            int lSysMenu;
            const int MF_BYPOSITION = 1024;
            lSysMenu = GetSystemMenu(this.Handle.ToInt32(), 0);
            RemoveMenu(lSysMenu,6,MF_BYPOSITION);
            child[0] = new Form_welcome();
            showChildForm(child[0]);
            this.label_usecompany.Text = this.label_usecompany.Text+ " "+ COMMAND.CommandBean.usecompany;
            this.label_nowuser.Text = this.label_nowuser.Text + " " + COMMAND.CommandBean.username;
            this.label_nowdata.Text = this.label_nowdata.Text + " " + COMMAND.CommandBean.nowtime;
        }
        public static bool closedflag = false;
        private void Form_main_FormClosing(object sender, FormClosingEventArgs e)
        {
            Application.Exit();
        }

        private void bt_exit_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定退出", "退出", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                closedflag = true;
                Application.Exit();
            }  
        }
        Form[] child = new Form[8];

        private void showChildForm(Form child)
        {
            child.MdiParent = this;
            this.panel_formload.Controls.Clear();
            this.panel_formload.Controls.Add(child);
            //child.Location = new Point(0, 0);
            child.Show();
        }
        private void bt_cailiaoguanli_Click(object sender, EventArgs e)
        {
            this.Text="进销存管理系统-材料管理";
            child[0] = new Form_cailiaoguanli();
            showChildForm(child[0]);
        }

        private void bt_chanpinguanli_Click(object sender, EventArgs e)
        {
            this.Text = "进销存管理系统-成品管理";
            child[1] = new Form_changpinguanli();
            showChildForm(child[1]);
        }

        private void bt_rukuguanli_Click(object sender, EventArgs e)
        {
            this.Text = "进销存管理系统-入库管理";
            child[2] = new Form_rukuguanli();
            showChildForm(child[2]);
        }

        private void bt_chukuguanli_Click(object sender, EventArgs e)
        {
            this.Text = "进销存管理系统-出库管理";
            child[3] = new Form_chukuguanli();
            showChildForm(child[3]);
        }

        private void bt_churukutaizhang_Click(object sender, EventArgs e)
        {
            this.Text = "进销存管理系统-出入库台账";
            child[4] = new Form_churukutaizhang();
            showChildForm(child[4]);
        }

        private void bt_changkuguanli_Click(object sender, EventArgs e)
        {
            this.Text = "进销存管理系统-库存管理";
            child[5] = new Form_kucunguanli();
            showChildForm(child[5]);
        }

        private void bt_chukumingxi_Click(object sender, EventArgs e)
        {
            this.Text = "进销存管理系统-出库明细";
            child[6] = new Form_cukumingxi();
            showChildForm(child[6]);
        }

        private void StripBtnUserExit_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定要退出系统？", "关闭提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                Application.Exit();
            }  
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            panel1.Left = panel1.Left - 3;
            if (panel1.Right < this.Left)
            {
                panel1.Left = this.Width;
            }
        }

        /**
         * 
         * 数据备份按钮按下
         * 
         * 弹出选择文件保存位置选择对话框，将数据库文件保存该位置
         * 
         * */
        private void toolStripButton1_Click(object sender, EventArgs e)
        {

            SaveFileDialog saveDia = new SaveFileDialog();
            //对话框样式设置
            saveDia.Filter = "数据文件(*.mdb)|*.mdb|所有文件(*.*)|*.*";
            saveDia.FilterIndex = 1;
            saveDia.RestoreDirectory = true;
            saveDia.FileName = "emsfina.mdb";
            saveDia.Title = "备份数据";
            saveDia.ValidateNames = true;

            //数据库文件位置，代码需要修改
            String fileNamePath = "E:\\emsfina.mdb";

            String uriString = "";

           //弹出对话框
            if (saveDia.ShowDialog() == DialogResult.OK)
            {
                uriString = saveDia.FileName;
            }
            else {
                return;
            }


            //调用保存功能
            saveFile(fileNamePath,uriString);
        }
        /**
         * 
         * 文件保存功能
         * 
         * 
         * 
         * */
        private void saveFile(String fileNamePath, String uriString)
        {

            //定义读取要保存文件的流
            FileStream fs = new FileStream(fileNamePath, FileMode.Open, FileAccess.Read);
            //转化成二进制读取
            BinaryReader r = new BinaryReader(fs);
            WebClient myWebClient  = new WebClient();
            myWebClient.Credentials = CredentialCache.DefaultCredentials;

            //字节数
            byte[] posArray = r.ReadBytes((int)fs.Length);

            //定义写数据流
            Stream postStream = myWebClient.OpenWrite(uriString, "PUT");

            try
            {
                if (postStream.CanWrite)
                {
                    //如果当前文件夹可写，则保存文件
                    postStream.Write(posArray, 0, posArray.Length);
                    postStream.Close();
                    fs.Dispose();
                    MessageBox.Show("数据备份成功！", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else {
                    postStream.Close();
                    fs.Dispose();
                    MessageBox.Show("数据备份失败，请重新备份！", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

            }
            catch (Exception err)
            {
                postStream.Close();
                fs.Dispose();
                MessageBox.Show("数据备份失败，请重新备份！", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally {
                postStream.Close();
                fs.Dispose();
            }

        }

        private void StripBtnUserManamge_Click(object sender, EventArgs e)
        {
            this.Text = "进销存管理系统-用户管理";
            child[7] = new Form_userManagement();
            showChildForm(child[7]);
        }

        private void panel_formload_Paint(object sender, PaintEventArgs e)
        {

        }

    }
}
